<!--
 * @Author: your name
 * @Date: 2021-01-10 21:16:07
 * @LastEditTime: 2021-01-31 23:37:10
 * @LastEditors: Please set LastEditors
 * @Description: 落地页面
 * @FilePath: \广告H5\src\page\land\index.vue
-->
<template>
  <div class="land-son" @touchstart="start" @touchend="end">
    <div class="generalize-poster">
      <div class="poster-tip" v-if="tipIsShow">
        <img class="ico" src="/static/images/land/ico2.png" />
        <span class="text">此二维码已通过安全验证，可以放心扫码</span>
        <img
          class="off"
          src="/static/images/land/off.png"
          @click="tipIsShow = false"
        />
      </div>
      <div
        :class="[
          index == activeIndex ? 'active-out' : '',
          index == activeIndex + 1 ? 'active-before' : '',
          index == activeIndex + 2 ? 'active' : '',
        ]"
        v-for="(item, index) in personList"
        :key="index"
        class="message-tip-list"
      >
        <span class="message-tip-pic"><img :src="item.headImg" /></span>
        <span class="message-tip-text">{{ item.content }}</span>
      </div>
      <div class="land-img">
        <img
          v-if="Object.keys(codeList).length"
          :src="IMGPATH + '/' + showCode"
        />
        <img
          v-for="item in contentList"
          :key="item.id"
          :src="IMGPATH + '/' + item.img_path"
        />
      </div>

      <div class="message-tip2">
        <span class="message-tip-text">{{ msg }}</span>
      </div>
    </div>
    <!--  <img
      v-if="Object.keys(codeList).length"
      class="qr-code"
      :src="IMGPATH + '/' + showCode"
    /> -->
    <!-- <div class="qr-code-line" v-if="route_path == 'land/test'"></div> -->
    <!-- <div class="qr-code-line"></div> -->
  </div>
</template>
<script>
import { personList } from "@/assets/js/json.js";
export default {
  created() {
 
    this.getCodeDetail();
    // this.getCodeList();
    let timer = setTimeout(() => {
      this.startActive(); //开始动画
      clearTimeout(timer);
    }, 2000);
    this.activeCountTimer = setInterval(this.activeCount, 1000);
  },
  data() {
    return {
      personList, //购买人员名单
      activeIndex: undefined, //动画开始索引
      activeIndexTimer: null, //动画定时器
      activeCountTimer: null, //倒计时定时器
      maxtime: 5 * 60,
      msg: "优惠剩余时间：" + "--" + " 分 " + "--" + " 秒",
      tipIsShow: true, //头部提示显示
      codeList: [], //二维码图片列表
      showCode: "", //当前展示二维码
      contentList: [], //内容图片列表
      route_path: "", //测试路径
    };
  },
  methods: {
    start() {
      clearTimeout(this.loop); //再次清空定时器，防止重复注册定时器
      this.loop = setTimeout(() => {
        // alert("12");
      }, 3000);
    },

    end() {
      clearTimeout(this.loop); //清空定时器，防止重复注册定时器
    },
    //根据路由查询相对的二维码数据
    getCodeDetail() {
      const currentRoute = this.$store.state.routeList.find(
        (item) => this.$route.path == "/" + item.page_route
      );
      this.route_path = currentRoute.page_route;
      this.getCodeList({ page_id: currentRoute.id });
      this.getPageContent({ page_id: currentRoute.id });
    },
    // 获取页面内容
    async getPageContent(param) {
      const { code, data } = await this.$api.getContent(param);
      if (!code) {
        this.contentList = data;
      }
    },
    // 获取二维码列表
    async getCodeList(param) {
      const { code, data } = await this.$api.getCodeList(param);
      if (!code) {
        this.codeList = data;
        this.judgeIsSave(); //随机获取一个二维码展示
      }
    },
    //获取当前展示的二维码 // 等概率取出一个二维码对象
    getShowCode() {
      const __rand__ = Math.random();
      const section = 1 / this.codeList.length; //每等分多少
      let initVal = 0; //初始值
      for (let i = 0; i <= this.codeList.length; i++) {
        if (__rand__ > initVal && __rand__ < initVal + section) {
          this.showCode = this.codeList[i].img_path;
          localStorage.setItem("code_id", this.codeList[i].id);
        }
        initVal += section;
      }
    },
    // 判断用户本地是否保存二维码  来决定存取
    judgeIsSave() {
      const code_id = localStorage.getItem("code_id");
      if (code_id) {
        let codeObj = this.codeList.find((item) => {
          return item.id == code_id;
        });
        codeObj ? (this.showCode = codeObj.img_path) : this.getShowCode();
      } else {
        this.getShowCode();
      }
    },
    //动画开始
    startActive() {
      this.activeIndex = 0;
      setInterval(() => {
        this.activeIndex < personList.length - 3
          ? this.activeIndex++
          : (this.activeIndex = 0);
      }, 2000);
    },
    // 优惠倒计时
    activeCount() {
      if (this.maxtime >= 0) {
        let minutes = Math.floor(this.maxtime / 60);
        let seconds = Math.floor(this.maxtime % 60);
        if (minutes < 10) {
          minutes = "0" + minutes;
        }
        if (seconds < 10) {
          seconds = "0" + seconds;
        }
        this.msg = "优惠剩余时间：" + minutes + " 分 " + seconds + " 秒";
        --this.maxtime;
      } else {
        clearInterval(timer);
      }
    },
  },
  beforeRouteLeave(to, from, next) {
    clearInterval(this.activeIndexTimer);
    clearInterval(this.activeCountTimer);
    next();
  },
};
</script>
<style lang='scss' scoped>
@keyframes qr-line {
  0% {
    top: 473px;
    box-shadow: 0 -1px 8px #d3695e;
  }
  50% {
    top: 565px;
    box-shadow: 0 4px 8px #d3695e;
  }
  100% {
    top: 473px;
    box-shadow: 0 -1px 8px #d3695e;
  }
}
.land-son {
  // 不准复制
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  position: relative;
  /* .qr-code {
    width: 121px;
    position: absolute;
    top: 440px;
    left: 45px;
  } */
  .qr-code-line {
    width: 133px;
    height: 3px;
    background-color: #d3695e;
    position: absolute;
    left: 210px;
    animation: qr-line linear 2.5s infinite;
  }
  .land-iframe {
    width: 100%;
    height: 100%;
    position: absolute;
    bottom: 0;
  }
  .generalize-poster {
    position: relative;
    width: 100vw;
    height: 100vh;
    background: #fff;
  }

  .generalize-poster .poster-tip {
    position: fixed;
    z-index: 99;
    top: 0;
    left: 0;
    width: 100vw;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-align: center;
    align-items: center;
    color: #01b90d;
    padding: 5px;
    box-sizing: border-box;
    font-size: 14px;
    background: rgba(0, 0, 0, 0.8);
  }
  .land-img {
    img {
      width: 100%;
      &:first-child {
        padding-top: 30px;
      }
      &:last-child {
        // padding-bottom: 20px;
      }
    }
  }

  .ico {
    width: 20px;
    margin-right: 10px;
  }

  .off {
    position: absolute;
    right: 30px;
    // top: 20px;
    width: 15px;
    cursor: pointer;
  }

  .message-tip2 {
    position: fixed;
    z-index: 9;
    bottom: 10px;
    left: 0;
    color: #fff;
    width: 100vw;
    height: 30px;
    overflow: hidden;
    line-height: 30px;
    box-sizing: border-box;
    background-color: #ce3426;
  }

  .message-tip2 .message-tip-text {
    display: inline-block;
    font-size: 16px;
    width: 100vw;
    text-align: center;
    overflow: hidden;
  }
  .message-tip-list {
    opacity: 0;
    position: absolute;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-align: center;
    align-items: center;
    -ms-flex-pack: distribute;
    justify-content: space-around;
    min-width: 212px;
    height: 23px;
    padding: 15px;
    box-sizing: border-box;
    border-radius: 20px;
    background-color: rgba(0, 0, 0, 0.5);
    color: #fff;
  }

  .message-tip-list .message-tip-pic {
    display: inline-block;
    width: 25px;
    height: 25px;
    line-height: 40px;
    border: 4px solid #fff;
    border-radius: 50%;
    margin-right: 20px;
    overflow: hidden;
    background-color: #333;
  }

  .message-tip-list .message-tip-text {
    -ms-flex: 1;
    flex: 1;
    font-size: 13px;
    line-height: 31px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .message-tip-list.active {
    opacity: 1;
    top: 100px;
    left: 24px;
    animation: scroll-data-v-1d0c841a 1s;
  }

  .message-tip-list.active-before {
    opacity: 1;
    top: 60px;
    left: 24px;
    animation: scrollBefore-data-v-1d0c841a 1s;
  }

  .message-tip-list.active-out {
    opacity: 0;
    top: 10px;
    left: 24px;
    animation: scrollOut-data-v-1d0c841a 0.8s;
  }

  img {
    width: auto;
  }

  @keyframes scroll-data-v-1d0c841a {
    0% {
      left: -24px;
    }

    to {
      left: 24px;
    }
  }

  @keyframes scrollBefore-data-v-1d0c841a {
    0% {
      top: 100px;
    }

    to {
      top: 60px;
    }
  }

  @keyframes scrollOut-data-v-1d0c841a {
    0% {
      top: 60px;
      opacity: 1;
    }

    to {
      top: 10px;
      opacity: 0;
    }
  }
}
</style>